package com.atguigu.realtime.tests

import java.text.SimpleDateFormat
import java.time.{Instant, LocalDateTime, ZoneId}
import java.time.format.DateTimeFormatter
import java.util.Date

/**
 * Created by Smexy on 2021/7/5
 *
 *    新的时间日期API： java.time.xxx
 *
 *    SimpleDateFormat  对应 DateTimeFormatter
 *    Date  对应  LocalDate
 *                LocalDateTime
 */
object TimeTransferTest {

  def main(args: Array[String]): Unit = {

    val ts: Long = System.currentTimeMillis()

    // 老的api，将ts转 日期和小时

    val format1 = new SimpleDateFormat("yyyy-MM-dd")
    val format2 = new SimpleDateFormat("hh")


    //将ts转换为 Date
    val date = new Date(ts)

    //将时间格式化
    val time1: String = format1.format(date)
    val time2: String = format2.format(date)

    println(time1)
    println(time2)

    println("--------------------oldAPI 有线程安全问题，效率低------------------")

    val formatter1: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
    val formatter2: DateTimeFormatter = DateTimeFormatter.ofPattern("hh")

    val localDateTime: LocalDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(ts), ZoneId.of("Asia/Shanghai"))

    println(localDateTime.format(formatter1))
    println(localDateTime.format(formatter2))


    println(localDateTime.getHour)



  }

}
